#include "dialoglogin.h"
#include "ui_dialoglogin.h"
#include <QMessageBox>
// #include <QGraphicsDropShadowEffect>

DialogLogin::DialogLogin(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DialogLogin)
{
    ui->setupUi(this);
    initUI();
}

void DialogLogin::initUI()
{
    this->setWindowFlag(Qt::FramelessWindowHint);
    installEventFilter(this);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    // QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    // shadowEffect->setBlurRadius(20);
    // shadowEffect->setXOffset(0);
    // shadowEffect->setYOffset(0);
    // shadowEffect->setColor(QColor(0, 0, 0, 51));
    // setGraphicsEffect(shadowEffect);
}


bool DialogLogin::eventFilter(QObject *obj, QEvent *event) {
    if (obj == this) {
        if (event->type() == QEvent::MouseButtonPress) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            if (mouseEvent->button() == Qt::LeftButton) {
                // 记录鼠标按下时的位置
                pressPos = mouseEvent->pos();
                // 启动定时器，检测长按
                QTimer::singleShot(500, this, [this]() {
                    if (isLongPress) {
                        // 长按后开始移动窗口
                        this->setCursor(Qt::ClosedHandCursor);
                    }
                });
                isLongPress = true;
            }
        } else if (event->type() == QEvent::MouseMove && isLongPress) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            // 计算窗口移动的偏移量
            QPoint delta = mouseEvent->pos() - pressPos;
            this->move(this->pos() + delta);
        } else if (event->type() == QEvent::MouseButtonRelease) {
            if (isLongPress) {
                // 释放鼠标时恢复光标并重置状态
                this->setCursor(Qt::ArrowCursor);
                isLongPress = false;
            }
        }
    }
    return QWidget::eventFilter(obj, event);
}

DialogLogin::~DialogLogin()
{
    delete ui;
}

void DialogLogin::on_pushButton_close_clicked()
{
    close();
}


void DialogLogin::on_pushButton_login_clicked()
{
    QString username = ui->lineEdit_account->text().trimmed();
    QString password = ui->lineEdit_password->text().trimmed();

    // 简单验证逻辑
    if (username == "admin" && password == "123456") {
        accept();  // 关闭对话框并返回 QDialog::Accepted
    } else {
        QMessageBox::critical(this,"警告","密码或用户名错误");
    }
}

